<template>
  <div class="password-container">
    <el-card class="password-card">
      <template #header>
        <div class="card-header">
          <span class="title">修改密码</span>
          <el-icon class="icon"><Lock /></el-icon>
        </div>
      </template>
      
      <el-form 
        label-width="120px" 
        size="large" 
        :model="form" 
        :rules="rules" 
        ref="pass"
        class="password-form"
      >
        <el-form-item label="原密码" prop="password">
          <el-input 
            v-model="form.password" 
            autocomplete="off" 
            show-password
            prefix-icon="el-icon-lock"
            placeholder="请输入原密码"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="新密码" prop="newPassword">
          <el-input 
            v-model="form.newPassword" 
            autocomplete="off" 
            show-password
            prefix-icon="el-icon-key"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input 
            v-model="form.confirmPassword" 
            autocomplete="off" 
            show-password
            prefix-icon="el-icon-check"
            placeholder="请再次输入新密码"
          ></el-input>
        </el-form-item>
        
        <el-form-item class="btn-container">
          <el-button type="primary" @click="save" class="submit-btn">
            <el-icon class="icon"><Check /></el-icon>
            确认修改
          </el-button>
          <el-button @click="resetForm" class="reset-btn">
            <el-icon class="icon"><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import request from '@/utils/request';
import { useRouter } from 'vue-router';
import { Lock, Check, Refresh } from '@element-plus/icons-vue';

const router = useRouter();
const userName = localStorage.getItem('userName');
const form = reactive({
  password: '',
  newPassword: '',
  confirmPassword: '',
  username: ''
});

const rules = {
  password: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 3, message: '长度不少于3位', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 3, message: '长度不少于3位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 3, message: '长度不少于3位', trigger: 'blur' }
  ]
};

const user = ref(localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {});
const pass = ref(null);

onMounted(() => {
  form.username = user.value.username;
});

const resetForm = () => {
  form.password = '';
  form.newPassword = '';
  form.confirmPassword = '';
};

const save = () => {
  pass.value.validate((valid) => {
    if (valid) {
      if (form.newPassword !== form.confirmPassword) {
        ElMessage.error("两次输入的新密码不相同");
        return false;
      }
      const userData = {
        password: form.password,
        newPassword: form.newPassword,
        username: userName
      }
      request.post("/user/password", userData).then(res => {
        if (res.data.code === '200') {
          ElMessage.success("修改成功");
          request.get('/user/logout')
          localStorage.removeItem('token')
          localStorage.removeItem('userName')
          localStorage.removeItem('menus')
          localStorage.removeItem('userType')
          router.push('/login')
        } else if(res.data.code == 401){
          ElMessage.warning(res.data.msg);
        }else{
          ElMessage.error(res.msg);
        }
      });
    }
  });
};
</script>

<style scoped>
.password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f5f7fa;
  padding: 20px;
}

.password-card {
  width: 550px;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15px 0;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #2c3e50;
  margin-right: 10px;
}

.icon {
  margin-right: 5px;
}

.password-form {
  padding: 20px 10px;
}

.el-form-item {
  margin-bottom: 25px;
}

.el-input {
  --el-input-hover-border: #409EFF;
}

.btn-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

.submit-btn {
  width: 140px;
  margin-right: 20px;
  background: linear-gradient(135deg, #409EFF, #3a8ee6);
  border: none;
  height: 45px;
  font-size: 16px;
  transition: transform 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.reset-btn {
  width: 140px;
  height: 45px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  background: #f5f7fa;
  border-color: #dcdfe6;
}

@media (max-width: 768px) {
  .password-card {
    width: 95%;
  }
  
  .btn-container {
    flex-direction: column;
    align-items: center;
  }
  
  .submit-btn, .reset-btn {
    width: 80%;
    margin: 10px 0;
  }
}
</style>
  